package com.itheima.demo;

import org.activiti.engine.*;
import org.activiti.engine.repository.ProcessDefinition;
import org.activiti.engine.runtime.ProcessInstance;
import org.activiti.engine.task.Task;
import org.junit.Test;

public class ActibitiBusynessDemo {

    @Test
    public void addbusinesskey()
    {
        ProcessEngine processEngine = ProcessEngines.getDefaultProcessEngine();

        RuntimeService runtimeService = processEngine.getRuntimeService();

        ProcessInstance processInstance = runtimeService.startProcessInstanceByKey("myEvection", "2501");

         System.out.println("添加的业务键为："+processInstance.getBusinessKey());

    }

    //暂停所有的任务     多个实例的挂起&&激活    暂停流程定义的所有内容
    @Test
    public void suspendallp(){

        //引用引擎
        ProcessEngine processEngine = ProcessEngines.getDefaultProcessEngine();

        //获取资源service
        RepositoryService repositoryService = processEngine.getRepositoryService();

        //查询出流程
        ProcessDefinition processDefinition = repositoryService.createProcessDefinitionQuery()
                .processDefinitionKey("myEvection")
                .singleResult();

        //判断流程是否暂停
        boolean definitionSuspended = processDefinition.isSuspended();

        //获取流程定义的id
        String definitionId = processDefinition.getId();

        if(definitionSuspended){
//            如果是挂起就变成激活
            repositoryService.activateProcessDefinitionById(definitionId,true,null);
            System.out.println("已激活");
        }else
        {
//            是激活状态就变为挂起
            repositoryService.suspendProcessDefinitionById(definitionId,true,null);
            System.out.println("一挂起");
        }
    }

    //暂停单个实例   :单个实例的挂起
    @Test
    public void suspendsingleinstance(){

        ProcessEngine processEngine = ProcessEngines.getDefaultProcessEngine();

        RuntimeService runtimeService = processEngine.getRuntimeService();

        ProcessInstance processInstance = runtimeService.createProcessInstanceQuery()
                .processInstanceId("2501")
                .singleResult();


        boolean instanceSuspended = processInstance.isSuspended();

        String instanceId = processInstance.getId();

        if (instanceSuspended){
            runtimeService.activateProcessInstanceById(instanceId);
            System.out.println("已经激活");
        }else{
            runtimeService.suspendProcessInstanceById(instanceId);
            System.out.println("已经挂起");
        }

    }


//    完成个人任务
@Test
    public void completetask(){
    ProcessEngine processEngine = ProcessEngines.getDefaultProcessEngine();

    TaskService taskService = processEngine.getTaskService();

    Task task = taskService.createTaskQuery()
            .processInstanceId("2501")
            .taskAssignee("zhangsan")
            .singleResult();
    System.out.println("任务id==="+task.getId());
    System.out.println("任务名称==="+task.getName());
    taskService.complete(task.getId());
}

}
